home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Mikael Andersson <prmiand@stog.wmdata.se>
- Subject: Re: file processing...
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <31514C27.58F3@stog.wmdata.se>
- Date: Thu, 21 Mar 1996 12:31:35 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <4iqr98$i3p@netnews.upenn.edu>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
- Organization: WM Data Infrateknik
-
- Sonny wrote:
- >
- > I want to read the 1st line of a text file into an array. The only way I know of to
- > detect the end of a line is by checking for "\n", but the code I wrote does not seem
- > be detecting it. Here is the code...
- >
- > #include<fstream.h>
- > #include<iostream.h>
- > #include<String.h>
- >
- > int readf(char array[],char *);
- > int readf (char array[], char *filename)
- > {
- > int i=0;
- > char c;
- >
- > ifstream inFile(filename, ios::in);
- > while (inFile >> c)
- > if (c!="\n") /*This line is giving me problem. It doesn't even compile */
- > array[i++]=c;
- > else
- > break;
- > return i;
- >
- > }
- >
- > void main(int argc,char *argv[])
- > {
- > int i;
- > char array[50];
- > i=readf(array,"gro");
- >
- > for(int c=0;c<i;c++)
- > cout<<array[c];
- > cout<<"\n";
- > }
- >
- > --Either you can use inFile.getline(buffer_pointer,max_length,'\n') (maybe
- readline I don't remember), or you can change to !='\n' or !=0x0A. I've
- never used the >> so I can be wrong but there's a inFile.read(...,1)
- which would read one character at a time so you could use that instead.
-
- Hope it helps, Micke.
-